package com.dd.cloud.auth.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dd.cloud.auth.entity.AdminRole;
import com.dd.cloud.auth.entity.RoleMenu;
import com.dd.cloud.auth.req.role.*;
import com.dd.cloud.auth.res.UserToRoleRes;
import com.dd.cloud.auth.service.IAdminPowerService;
import com.dd.cloud.auth.service.IAdminRoleService;
import com.dd.cloud.auth.service.IRoleMenuService;
import com.dd.cloud.auth.service.IUserToRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lx
 * @version 1.0
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/power")
public class RoleController {
    @Autowired
    private IAdminPowerService adminPowerService;
    @Autowired
    private IRoleMenuService roleMenuService;
    @Autowired
    private IUserToRoleService userToRoleService;
    @Autowired
    private IAdminRoleService adminRoleService;


    /**
     * 这个url只是用作验证uri是否有权限的功能, uri存放于req参数中
     * 用作网关调用验证当前用户调用其他服务的uri是否有权限调用
     * 不做实际的业务处理逻辑
     *
     * @return
     */
    @GetMapping("/check/role/url")
    public ResponseEntity<Map<String, String>> checkRoleToUrl(@RequestParam("uri") String uri) {
        return ResponseEntity.ok(new HashMap<>());
    }


    /**
     * 查询系统菜单
     *
     * @return
     */
    @GetMapping("/menu/tree/roleId")
    public ResponseEntity<List<RoleMenu>> queryMenuTreeByRoleId(Integer roleId) {
        List<RoleMenu> list = roleMenuService.queryMenuTreeByUserId(roleId);
        return ResponseEntity.ok(list);
    }

    /**
     * 查询系统菜单
     *
     * @return
     */
    @GetMapping("/menu/tree")
    public ResponseEntity<List<RoleMenu>> queryMenuTree(@NotNull(message = "标识不能为空") Integer flag) {
        List<RoleMenu> list = adminPowerService.queryMenuTree(flag);
        return ResponseEntity.ok(list);
    }

    /**
     * 查询系统菜单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/menu/id")
    public ResponseEntity<RoleMenu> queryMenuById(@NotNull(message = "菜单id不能为空") Integer id) {
        RoleMenu roleMenu = adminPowerService.queryMenuById(id);
        return ResponseEntity.ok(roleMenu);
    }

    /**
     * 查询角色的菜单
     *
     * @param roleId
     * @return
     */
    @GetMapping("/menu/roleId")
    public ResponseEntity<List<RoleMenu>> queryRoleMenuByRoleId(@NotNull(message = "角色id不能为空") Integer roleId) {
        List<RoleMenu> list = adminPowerService.queryRoleMenuByRoleId(roleId);
        return ResponseEntity.ok(list);
    }

    /**
     * 查询角色的菜单
     *
     * @return
     */
    @GetMapping("/user/role/list")
    public ResponseEntity<List<UserToRoleRes>> userRoleList() {
        List<UserToRoleRes> list = userToRoleService.getUserRoleList();
        return ResponseEntity.ok(list);
    }

    /**
     * 查询用户是否超管
     *
     * @param userId
     * @return
     */
    @GetMapping("/user/isAdmin")
    public ResponseEntity<Integer> userIsAdmin(@NotNull(message = "角色id不能为空") Integer userId,
                                               Integer userType) {
        Integer integer = adminPowerService.queryRoleIsAdmin(userId, userType);
        return ResponseEntity.ok(integer);
    }

    /**
     * 用户绑定角色
     *
     * @param req
     * @return
     */
    @PostMapping("/user/bind/role")
    public ResponseEntity<Integer> userBindRole(@RequestBody UserBindRoleReq req) {
        adminPowerService.bindUserRole(req.getUserId(), req.getRoleId(), req.getType(), req.getRoleName());
        return ResponseEntity.ok().build();
    }

    /**
     * 查询管理员的菜单
     *
     * @param userId
     * @return
     */
    @GetMapping("/menu/userId")
    public ResponseEntity<List<RoleMenu>> queryMenus(@NotNull(message = "标识不能为空") Integer flag,
                                                     @NotNull(message = "管理员id不能为空") Integer userId) {
        List<RoleMenu> list = adminPowerService.queryUserMenus(flag, userId);
        return ResponseEntity.ok(list);
    }

    /**
     * 修改菜单
     *
     * @param rm
     */
    @PostMapping("/menu/update")
    public ResponseEntity<Void> updateRoleMenu(@Valid @RequestBody RoleMenuUpdateReq rm) {
        adminPowerService.updateRoleMenu(rm);
        return ResponseEntity.ok().build();
    }

    /**
     * 添加菜单
     *
     * @param rm
     */
    @PostMapping("/menu/add")
    public ResponseEntity<Void> addRoleMenu(@Valid @RequestBody RoleMenuAddReq rm) {
        adminPowerService.addRoleMenu(rm);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询管理系统角色
     *
     * @return
     */
    @GetMapping("/role/list/all")
    public ResponseEntity<List<AdminRole>> queryAdminRoleListAll() {
        List<AdminRole> res = adminPowerService.queryAdminRoleListAll();
        return ResponseEntity.ok(res);
    }

    /**
     * 查询管理系统角色
     *
     * @return
     */
    @GetMapping("/role/list")
    public ResponseEntity<IPage<AdminRole>> queryAdminRoleList(@Valid RoleQueryTermsReq req) {
        IPage<AdminRole> res = adminPowerService.queryAdminRoleList(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 添加角色
     *
     * @param req
     */
    @PostMapping("/role/add")
    public ResponseEntity<Void> addRoleMenu(@Valid @RequestBody RoleCreateReq req) {
        adminPowerService.addAdminRole(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改角色
     *
     * @param req
     */
    @PostMapping("/role/update")
    public ResponseEntity<Void> updateRoleMenu(@Valid @RequestBody RoleUpdateReq req) {
        adminPowerService.updateAdminRole(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询角色详情
     *
     * @param roleId
     * @return
     */
    @GetMapping("/role/detail")
    public ResponseEntity<AdminRole> queryAdminRoleDetail(@NotNull(message = "角色ID不能为空") Integer roleId) {
        AdminRole ar = adminPowerService.queryAdminRoleDetail(roleId);
        return ResponseEntity.ok(ar);
    }

    /**
     * 查询用户角色信息
     */
    @GetMapping("/user/role/info")
    public ResponseEntity<AdminRole> queryRoleByUserId(@RequestParam(name = "userId") Integer userId,
                                                       @RequestParam(name = "type") Integer type) {
        AdminRole AdminRole = adminPowerService.queryRoleByUserId(userId, type);
        return ResponseEntity.ok(AdminRole);
    }
}
